package com.bilibili.controller;

import com.bilibili.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/findById/{id}")
    @HystrixCommand(fallbackMethod = "fallbackGetById")
    public User findById(@PathVariable("id") Long id){

        // 使用restTemplate发送
        String url = "http://user-server/user/findById/" + id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    /**
     * 兜底方案
     * 真正的接口调用报错后会走这里 ：上面的user调用失败的情况
     * @param id
     * @return
     */
    public User fallbackGetById(@PathVariable("id") Long id){

        return new User(-1L,"无","用户服务不可到达");//尝试计算调用时间,引出饥饿加载
    }
}
